import {debounce} from "./utils";
import BackTop from "components/content/backTop/BackTop";
import {TOP_DISTANCE} from "./const";
import {POP, NEW, SELL} from "./const";

export const itemImgListenerMixin = {
  data() {
    return {
      itemImgListener: null
    }
  },
  mounted() {
    const refresh = debounce(this.$refs.scroll.refresh, 200)
    this.itemImgListener = () => {
      refresh()
    }
    // 发出者在 GoodsListItem 组件中
    this.$bus.$on('itemImgLoad', this.itemImgListener)
    // console.log('我是混入中的内容');
  }
}

export const categoryImgListenerMixin = {
  data() {
    return {
      tabOffsetTop: 0,  // 滚动区域滚动的距离
      categoryImgListener: null
    }
  },
  mounted() {
    const getTabOffsetTop = debounce(() => {
      this.tabOffsetTop = this.$refs.tabControl2.$el.offsetTop
    }, 200)
    this.categoryImgListener = () => {
      getTabOffsetTop()
    }
    // 发出者在 TabContentCategory 组件中
    this.$bus.$on('categoryImgLoad', this.categoryImgListener)
  }
}

export const backTopMixin = {
  components: {
    BackTop
  },
  data() {
    return {
      isShowBackTop: false
    }
  },
  methods: {
    backClick() {
      this.$refs.scroll.scrollTo(0, 0, 300);
    },
    listenShowBackTop(position) {
      this.isShowBackTop = position.y <= -TOP_DISTANCE
    }
  }
}

export const tabControlMixin = {
  data: function () {
    return {
      currentType: POP
    }
  },
  methods: {
    tabControlClick(index) {
      switch (index) {
        case 0:
          this.currentType = POP
          break
        case 1:
          this.currentType = NEW
          break
        case 2:
          this.currentType = SELL
          break
      }
    }
  }
}
